//
//  WDScanCodeBottomView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/7.
//

#import "WDScanCodeBottomView.h"
#import "WDScanCodeViewModel.h"
#import "WDScanCodeButton.h"

@interface WDScanCodeBottomView ()
@property (nonatomic, strong) WDScanCodeViewModel *viewModel;
@property (nonatomic, strong) WDScanCodeButton *recordBtn;
@property (nonatomic, strong) UILabel *tipLabel;

@property (nonatomic, strong) UILabel *inputTipLabel;

/// 手动输入
@property (nonatomic, strong) UIButton *btnInput;


@end

@implementation WDScanCodeBottomView

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor orangeColor];
    
//    self.backgroundColor = [UIColor systemPinkColor];
    
//    [self initWithTipLabel];
    
    
}

- (void)initWithTipLabel {
    if (!_tipLabel) {
        CGRect frame = CGRectMake(self.viewModel.scanRect.origin.x, 40, self.width - self.viewModel.scanRect.origin.x * 2, 20);
        _tipLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"将二维码/条码放入框内,即可自动扫描";
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.numberOfLines = 0;
        }];
    }
}

- (void)initWithInputTipLabel {
    if (!_inputTipLabel) {
        CGRect frame = CGRectMake(0, self.height - 18 - 8, self.width, 18);
        _inputTipLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
            label.text = @"无法扫码时点击手动输入";
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
}

- (void)initWithBtnInput {
    if (!_btnInput) {
        UIImage *image = [UIImage imageNamed:@"ic_scan_input_edit"];
        CGRect frame = CGRectMake((self.width - 56) * 0.5, self.inputTipLabel.top - 15 - 56, 56, 56);
        _btnInput = [UIButton createWithButton:self frame:frame font:FONTSIZE(16) target:self action:@selector(inputBtnEvent:) propertiesBlock:^(UIButton * _Nonnull button) {
            button.backgroundColor = [UIColor colorWithHexString:@"#242424"];
            button.layer.cornerRadius = frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
}

- (void)setTipInfo:(NSString *)tipInfo {
    if ([objectNull(tipInfo) isEqualToString:@""]) {
        return;
    }
    CGRect frame = self.tipLabel.frame;
    UIFont *font = self.tipLabel.font;
    float height = [tipInfo heightForFont:font width:frame.size.width];
    if (height < 20) {
        height = 20;
    }
    self.tipLabel.height = height;
    self.tipLabel.text = objectNull(tipInfo);
}

- (void)initWithRecordBtn {
    if (!_recordBtn) {
        //手动输入button
        WDScanCodeButton *inputBtn = [WDScanCodeButton buttonWithType:UIButtonTypeCustom];
        self.recordBtn = inputBtn;
        [inputBtn setImage:[self.viewModel imageWithName:@"icon_enterinput"] forState:UIControlStateHighlighted];
        [inputBtn setImage:[self.viewModel imageWithName:@"icon_enterinput"] forState:UIControlStateNormal];
        [inputBtn addTarget:self action:@selector(inputBtnEvent:) forControlEvents:UIControlEventTouchUpInside];

        if (self.viewModel.isShowLightBtn && self.viewModel.isShowInputBtn) { //两个按钮
           inputBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - LightBtnMarginInputBtn - 2*InPutAndLightBtnWidth) * 0.5, 0 + 45, InPutAndLightBtnWidth, InPutAndLightBtnHeight);
            //开关灯button
            WDScanCodeButton *turnBtn = [WDScanCodeButton buttonWithType:UIButtonTypeCustom];
            turnBtn.frame = CGRectMake(CGRectGetMaxX(inputBtn.frame) + LightBtnMarginInputBtn, 0 + 45, InPutAndLightBtnWidth, InPutAndLightBtnHeight);
            [turnBtn setImage:[self.viewModel imageWithName:@"icon_light_normal"] forState:UIControlStateNormal];
            [turnBtn setImage:[self.viewModel imageWithName:@"icon_light_selected"] forState:UIControlStateSelected];
            [turnBtn setTitle:@"轻触照亮" forState:UIControlStateNormal]; // 打开手电筒
            [turnBtn setTitle:@"轻触关闭" forState:UIControlStateSelected]; // 关闭手电筒
            [turnBtn addTarget:self action:@selector(turnBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
            [inputBtn setTitle:@"手动输入" forState:UIControlStateNormal];
            [inputBtn setTitle:@"手动输入" forState:UIControlStateHighlighted];
          
            [self addSubview:inputBtn];
            [self addSubview:turnBtn];
            
        }else if (self.viewModel.isShowInputBtn && !self.viewModel.isShowLightBtn){
            //显示一个按钮
            inputBtn.frame = CGRectMake((self.width - InPutAndLightBtnWidth) * 0.5, 0 + 45, InPutAndLightBtnWidth, InPutAndLightBtnHeight);
            [inputBtn setTitle:@"手动输入" forState:UIControlStateNormal];
            [inputBtn setTitle:@"手动输入" forState:UIControlStateHighlighted];
            [self addSubview:inputBtn];
        }else if (!self.viewModel.isShowInputBtn && !self.viewModel.isShowLightBtn) {
            //两个按钮都不显示
        }
    }
}

/// 手动输入
/// - Parameter sender: sender description
- (void)inputBtnEvent:(id)sender {
    if (self.touchInputEventBlock) {
        self.touchInputEventBlock();
    }
}

/// 打开关闭手电筒
/// - Parameter sender: sender description
- (void)turnBtnEvent:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (self.touchTurnTorchBlock) {
        self.touchTurnTorchBlock(btn.selected);
    }
}

- (void)updateWithViewData:(id)data
{
    self.viewModel = data;
    [self initWithTipLabel];
//    [self initWithRecordBtn];
    
    [self initWithInputTipLabel];
    
    [self initWithBtnInput];
}

@end
